package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.waypolice.common.pojo.EasyUiDataGridResult;
import com.waypolice.manager.service.LogOperateService;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.mapper.UpmsLogMapper;
import com.waypolice.pojo.UpmsLog;
import com.waypolice.pojo.UpmsLogExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author wangrang
 * @Date Created in 16:26 2018/10/10
 * @Modified By:
 */
@Service
public class LogOperateServiceImpl implements LogOperateService {


    @Autowired
    private UpmsLogMapper upmsLogMapper;

    @Override
    public int insertUpmsLogSelective(UpmsLog upmsLog) {
        int i = upmsLogMapper.insertSelective(upmsLog);
        return i;
    }

    @Override
    public EasyUiDataGridResult findLogPageList(int page, int rows) {
        //设置分页信息
        PageHelper.startPage(page, rows);
       //执行查询
        UpmsLogExample upmsLogExample = new UpmsLogExample();
        UpmsLogExample.Criteria criteria = upmsLogExample.createCriteria();
        //全部查询时，只查当日全部
        long timesmorning = DateTimeUtil.getTimesmorning();
        long timesnight = DateTimeUtil.getTimesnight();
        criteria.andStartTimeBetween(timesmorning,timesnight);
        upmsLogExample.setOrderByClause("'log_id' desc");
        List<UpmsLog> upmsLogs = upmsLogMapper.selectByExample(upmsLogExample);

        //取查询结果
        PageInfo<UpmsLog> pageInfo = new PageInfo<>(upmsLogs);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setRows(upmsLogs);
        result.setTotal((int)pageInfo.getTotal());
        return result;

    }

    @Override
    public EasyUiDataGridResult findLogPageList(HashMap<String, Object> hashMap, int page, int rows) {
        //设置分页信息
        PageHelper.startPage(page, rows);
        //执行查询
        UpmsLogExample upmsLogExample = new UpmsLogExample();
        UpmsLogExample.Criteria criteria = upmsLogExample.createCriteria();
        if(hashMap.get("startDate_log") != null && hashMap.get("endDate_log") !=null ){
            Long startDate_log = (Long) hashMap.get("startDate_log");
            Long endDate_log = (Long) hashMap.get("endDate_log");
            criteria.andStartTimeBetween(startDate_log,endDate_log);//上报时间在这个范围之内
        }
        if(StringUtils.isNotBlank(hashMap.get("dlzh_log").toString())){
            criteria.andUsernameLike("%"+(String) hashMap.get("dlzh_log")+"%");
        }
        if(StringUtils.isNotBlank((String)hashMap.get("ip_log"))){
            criteria.andIpEqualTo(hashMap.get("ip_log").toString());
        }

        upmsLogExample.setOrderByClause("'log_id' desc");

        List<UpmsLog> upmsLogs = upmsLogMapper.selectByExample(upmsLogExample);

        //取查询结果
        PageInfo<UpmsLog> pageInfo = new PageInfo<>(upmsLogs);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setRows(upmsLogs);
        result.setTotal((int)pageInfo.getTotal());
        return result;
    }
}
